<template>

<!--  销售退货付款 停用-->

  <div class="app-container">
    <!-- 搜索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单据编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据日期">
        <el-date-picker
          v-model="daterangeOrderDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select
          v-model="queryParams.customerId"
          clearable
          placeholder="请选择客户"
        >
          <el-option
            :label="customer.cusName"
            :value="customer.id"
            v-for="customer in customerList"
            :key="customer.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生效日期">
        <el-date-picker
          v-model="daterangeEffectDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否生效" prop="isEffect">
        <el-select
          v-model="queryParams.isEffect"
          clearable
          placeholder="请选择是否生效"
        >
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="item in isEffectList"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否关闭" prop="isClose">
        <el-select
          v-model="queryParams.isClose"
          clearable
          placeholder="请选择是否关闭"
        >
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="item in isCloseList"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间">
        <el-date-picker
          v-model="daterangeModifyDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>
    <!-- 中部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddSale"
          v-hasPermi="['finance:reciveOrder:add']"
        >新增销售收款单
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['finance:reciveOrder:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- 中部数据表格 -->
    <el-table
      v-loading="loading"
      :data="reciveOrderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" width="70" align="center" fixed="left">
        <template slot-scope="scope">
          {{
            scope.$index + 1 + (queryParams.pageNum - 1) * queryParams.pageSize
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="单据编号"
        align="center"
        prop="orderCode"
        fixed="left"
        width="150"
      >
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            width="150"
            trigger="hover"
            :content="scope.row.orderCode"
          >
            <div slot="reference" class="ellipsis-text">
              <el-link type="primary" @click="showReciveOrder(scope.row)">
                {{ scope.row.orderCode }}
              </el-link
              >
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        label="单据类型"
        align="center"
        prop="paymentTypeName"
        width="120"
      >
      </el-table-column>
      <el-table-column
        label="单据日期"
        align="center"
        prop="orderDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据阶段" align="center" prop="orderStatusName">
      </el-table-column>
      <el-table-column label="客户" align="center" prop="customer.cusName"/>
      <el-table-column
        label="审核人"
        align="center"
        prop="approvalUser.userName"
      />
      <el-table-column
        label="审核结果"
        align="center"
        prop="approvalResultName"
      >
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="approvalOption"/>
      <el-table-column
        label="生效日期"
        align="center"
        prop="effectDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否生效" align="center" prop="isEffectName">
      </el-table-column>
      <el-table-column label="金额" align="center" prop="money"/>
      <el-table-column
        label="已核销金额"
        align="center"
        prop="verMoney"
        width="120"
      />
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="modifyBy"/>
      <el-table-column
        label="修改时间"
        align="center"
        prop="modifyDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:reciveOrder:edit']"
            :disabled="scope.row.orderStatus != 2"
          >修改
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:reciveOrder:remove']"
            :disabled="scope.row.approvalResult != 3"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售收款单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :disabled="formDisabled"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="销售收款编号" prop="orderCode">
              <el-input
                v-model="form.orderCode"
                placeholder="请输入销售收款编号"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单据日期" prop="orderDate">
              <el-date-picker
                clearable
                v-model="form.orderDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择单据日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户" prop="customerId">
              <el-select
                v-model="form.customerId"
                clearable
                placeholder="请选择客户"
              >
                <el-option
                  :label="customer.cusName"
                  :value="customer.id"
                  v-for="customer in customerList"
                  :key="customer.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="单据类型" prop="paymentType">
              <el-select
                :value="form.paymentType"
                disabled
                clearable
                placeholder="请选择单据类型"
              >
                <el-option
                  v-for="paymentType in paymentTypeList"
                  :key="paymentType.id"
                  :label="paymentType.name"
                  :value="paymentType.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <i
          class="el-icon-arrow-down"
          @click="showFormDetail"
          style="cursor: pointer"
        ></i>
        <el-divider></el-divider>
        <div v-show="formDetailDisabled">
          <el-row>
            <el-col :span="8">
              <el-form-item label="审核人" prop="approvalUser.userName">
                <el-input
                  :value="form.approvalUser.userName"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审核结果" prop="approvalResultName">
                <el-input
                  :value="form.approvalResultName"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审核意见" prop="approvalOption">
                <el-input
                  type="textarea"
                  :value="form.approvalOption"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="金额" prop="money">
                <el-input :value="form.money" disabled placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="已核销金额" prop="verMoney">
                <el-input :value="form.verMoney" disabled placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="生效日期" prop="effectDate">
                <el-date-picker
                  clearable
                  :value="form.effectDate"
                  type="date"
                  disabled
                  value-format="yyyy-MM-dd"
                  placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="创建人" prop="createBy">
                <el-input :value="form.createBy" placeholder="" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="修改人" prop="modifyBy">
                <el-input :value="form.modifyBy" placeholder="" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="创建时间" prop="createDate">
                <el-date-picker
                  clearable
                  :value="form.createDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="修改时间" prop="modifyDate">
                <el-date-picker
                  clearable
                  :value="form.modifyDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单据阶段" prop="orderStatusName">
                <el-input
                  :value="form.orderStatusName"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="remark">
                <el-input
                  type="textarea"
                  v-model="form.remark"
                  placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          @select="handleDetailSelect"
        >
          <el-menu-item index="1">销售订单</el-menu-item>
          <el-menu-item index="2">明细</el-menu-item>
        </el-menu>
        <el-col :span="1.5">
          <el-popover
            placement="top-start"
            width="200"
            trigger="hover"
            content="请先选择客户"
          >
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              slot="reference"
              @click="showSaleOrder"
              v-show="activeIndex == '2'"
              :disabled="!this.form.customerId"
            >销售退货订单
            </el-button
            >
          </el-popover>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="orderMultiple"
            @click="handleDetailDelete"
            v-show="activeIndex == '2'"
          >删除
          </el-button
          >
        </el-col>
        <el-table
          ref="saleOrderDetailTable"
          :data="saleOrderDetailList"
          tooltip-effect="dark"
          style="width: 100%"
          v-show="activeIndex == '1'"
        >
          <el-table-column fixed prop="saleBackCode" label="单据编号" width="150">
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                width="200"
                trigger="hover"
                :content="scope.row.saleBackCode"
              >
                <div slot="reference" class="ellipsis-text">
                  {{ scope.row.code }}
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="createDate" label="单据日期" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createDate, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="total" label="结算金额" width="120">
          </el-table-column>
          <el-table-column prop="payMoney" label="已结算金额" width="120">
          </el-table-column>
          <el-table-column prop="remark" label="备注" width="120">
          </el-table-column>
        </el-table>
        <el-table
          ref="reciveOrderDetailTable"
          :data="form.reciveOrderDetailList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleDetailSelectionChange"
          v-show="activeIndex == '2'"
          show-summary
          highlight-current-row
        >
          <el-table-column type="selection" width="70" align="center"/>
          <el-table-column
            type="index"
            label="序号"
            width="70"
            align="center"
          />

          <el-table-column prop="srcOrderCode" label="源单号" width="150">
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                width="150"
                trigger="hover"
                :content="scope.row.srcOrderCode"
              >
                <div slot="reference" class="ellipsis-text">
                  {{ scope.row.srcOrderCode }}
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column prop="payTypeId" label="结算方式" width="120">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.payTypeId"
                placeholder="请选择结算方式"
              >
                <el-option
                  v-for="payType in payTypeList"
                  :key="payType.id"
                  :label="payType.name"
                  :value="payType.id"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column prop="accountId" label="资金账户" width="120">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.accountId"
                placeholder="请选择资金账户"
              >
                <el-option
                  v-for="account in accountList"
                  :key="account.id"
                  :label="account.name"
                  :value="account.id"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column prop="money" label="金额" width="120">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.money"
                placeholder="请输入金额"
                @blur="validateMoney(scope.row)"
              ></el-input>
            </template>
          </el-table-column>

          <el-table-column prop="remark" label="备注" width="120">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.remark"
                placeholder="请输入备注"
              ></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <!-- 销售订单内部对话框 -->
      <el-dialog
        width="60%"
        title="选择销售订单"
        :visible.sync="innerVisible"
        append-to-body
      >
        <!-- 搜索头部 -->
        <el-form
          :model="querySaleOrderParams"
          ref="querySaleOrderForm"
          size="small"
          :inline="true"
          v-show="showSaleOrderSearch"
          label-width="68px"
        >
          <el-form-item label="单据编号" prop="code">
            <el-input
              v-model="querySaleOrderParams.code"
              placeholder="请输入单据编号"
              clearable
              @keyup.enter.native="handleSaleOrderQuery"
            />
          </el-form-item>
          <el-form-item label="单据日期">
            <el-date-picker
              v-model="daterangeSaleOrderCreateDate"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleSaleOrderQuery"
            >搜索
            </el-button
            >
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="resetSaleOrderQuery"
            >重置
            </el-button
            >
          </el-form-item>
        </el-form>
        <el-table
          ref="saleOrderTable"
          v-loading="saleOrderLoading"
          :data="saleOrderList"
          @selection-change="handleSaleOrderSelectionChange"
          row-key="id"
        >
          <el-table-column type="selection" width="70" align="center"/>
          <el-table-column label="序号" width="70" align="center" fixed>
            <template slot-scope="scope">
              {{
                scope.$index +
                1 +
                (querySaleOrderParams.pageNum - 1) *
                querySaleOrderParams.pageSize
              }}
            </template>
          </el-table-column>
          <el-table-column fixed prop="srcOrderCode" label="单据编号" width="150">
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                width="200"
                trigger="hover"
                :content="scope.row.srcOrderCode"
              >
                <div slot="reference" class="ellipsis-text">
                  {{ scope.row.srcOrderCode }}
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="createDate" label="单据日期" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createDate, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="cusName" label="客户" width="120">
          </el-table-column>
          <el-table-column prop="createBy" label="业务员" width="120">
          </el-table-column>
          <el-table-column prop="realMoney" label="结算金额" width="120">
          </el-table-column>
          <el-table-column prop="payMoney" label="已结算金额" width="120">
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination
          v-show="saleOrderTotal > 0"
          :total="saleOrderTotal"
          :page.sync="querySaleOrderParams.pageNum"
          :limit.sync="querySaleOrderParams.pageSize"
          @pagination="getSaleOrderList"
        />
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleDetailAdd">确 定</el-button>
          <el-button @click="cancelInner">取 消</el-button>
        </div>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formDisabled"
        >确 定
        </el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReciveOrder,
  getReciveOrder,
  delReciveOrder,
  addReciveOrder,
  updateReciveOrder,
  listPayType,
} from "@/api/finance/reciveOrder";
import {getAllCus} from "@/api/basedata/customer";
import {listAllSupplies} from "@/api/basedata/supplies";
import {listAllAccount} from "@/api/finance/account";
import {listForReciveOrder} from "@/api/xiaoshou/saleOrder";
import {getSaleOrderListByOrderCodes} from "@/api/xiaoshou/saleOrder";
import request from "@/utils/request";

export default {
  name: "ReciveOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 财务收款单表格数据
      reciveOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 采购退货单删除按钮禁用
      orderMultiple: true,
      // 备注时间范围
      daterangeOrderDate: [],
      // 备注时间范围
      daterangeEffectDate: [],
      // 备注时间范围
      daterangeCreateDate: [],
      // 备注时间范围
      daterangeModifyDate: [],
      // 客户列表
      customerList: [],
      // 供应商列表
      supplierList: [],
      //支付方式列表
      payTypeList: [],
      // 财务收款单类型列表
      paymentTypeList: [
        {id: 2, name: "销售收款单"},
        {id: 3, name: "采购退货收款单"},
        {id: 4, name: "其他收款单"},
        {id: 8, name: "销售退货付款单"},
      ],
      // 单据阶段列表
      orderStatusList: [
        {
          id: 2,
          name: "已提交",
        },
        {
          id: 3,
          name: "已审批",
        },
        {
          id: 4,
          name: "执行完",
        },
      ],
      // 审批结果列表
      approvalList: [
        {
          id: 1,
          name: "未审批",
        },
        {
          id: 2,
          name: "已通过",
        },
        {
          id: 3,
          name: "已驳回",
        },
      ],
      //是否生效
      isEffectList: [
        {
          id: 0,
          name: "未生效",
        },
        {
          id: 1,
          name: "已生效",
        },
      ],
      // 是否关闭
      isCloseList: [
        {
          id: 0,
          name: "未关闭",
        },
        {
          id: 1,
          name: "已关闭",
        },
      ],
      //是否禁用表单
      formDisabled: false,
      // 销售出库单明细表格列表
      saleOrderDetailList: [],
      // 内部销售出库对话框控制
      innerVisible: false,
      // 内部销售出库对话框搜索控制
      showSaleOrderSearch: true,
      // 内部单据日期范围
      daterangeSaleOrderCreateDate: [],
      // 内部销售出库单选择列表
      saleOrderList: [],
      // 内部销售出库单中已选中的数据
      tempDetailList: [],
      // 内部销售出库单搜索参数
      querySaleOrderParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        orderDate: null,
        cusName: null,
      },
      // 内部销售出库单加载状态
      saleOrderLoading: false,
      // 内部销售出库单总条数
      saleOrderTotal: 3,
      // 明细切换
      activeIndex: "2",
      // 表单详细数据
      formDetailDisabled: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        orderDate: null,
        orderStatus: null,
        customerId: null,
        suppliesId: null,
        effectDate: null,
        isEffect: null,
        isClose: null,
        isCancel: null,
        createDate: null,
        modifyDate: null,
        paymentType: 8,
      },
      // 表单参数
      form: {
        reciveOrderDetailList: [],
        approvalUser: {userName: ""},
      },
      // 表单校验
      rules: {
        orderCode: [
          {required: true, message: "单据编号不能为空", trigger: "blur"},
        ],
        orderDate: [
          {required: true, message: "单据日期不能为空", trigger: "blur"},
        ],
        customerId: [
          {required: true, message: "客户不能为空", trigger: "blur"},
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getCustomerList();
    this.getSupplierList();
    this.getPayTypeList();
    this.getAccountList();
  },
  methods: {
    //获取所有账户列表
    getAccountList() {
      listAllAccount().then((response) => {
        this.accountList = response.data;
      });
    },
    //获取支付方式列表
    getPayTypeList() {
      listPayType().then((response) => {
        this.payTypeList = response.data;
      });
    },
    //获取所有供货商
    getSupplierList() {
      listAllSupplies().then((response) => {
        this.supplierList = response.data;
      });
    },
    /** 获取客户列表 */
    getCustomerList() {
      getAllCus().then((response) => {
        this.customerList = response.data;
      });
    },
    /** 查询财务收款单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderDate && "" != this.daterangeOrderDate) {
        this.queryParams.params["beginOrderDate"] = this.daterangeOrderDate[0];
        this.queryParams.params["endOrderDate"] = this.daterangeOrderDate[1];
      }
      if (null != this.daterangeEffectDate && "" != this.daterangeEffectDate) {
        this.queryParams.params["beginEffectDate"] =
          this.daterangeEffectDate[0];
        this.queryParams.params["endEffectDate"] = this.daterangeEffectDate[1];
      }
      if (null != this.daterangeCreateDate && "" != this.daterangeCreateDate) {
        this.queryParams.params["beginCreateDate"] =
          this.daterangeCreateDate[0];
        this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
      }
      if (null != this.daterangeModifyDate && "" != this.daterangeModifyDate) {
        this.queryParams.params["beginModifyDate"] =
          this.daterangeModifyDate[0];
        this.queryParams.params["endModifyDate"] = this.daterangeModifyDate[1];
      }
      listReciveOrder(this.queryParams).then((response) => {
        this.reciveOrderList = response.rows;
        console.log("list", this.reciveOrderList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        paymentType: 8,
        orderDate: null,
        orderStatus: null,
        customerId: null,
        suppliesId: null,
        approvalId: null,
        approvalResult: null,
        approvalOption: null,
        effectDate: null,
        isEffect: null,
        isClose: null,
        isCancel: null,
        money: null,
        verMoney: null,
        remark: null,
        createBy: null,
        createDate: null,
        modifyBy: null,
        modifyDate: null,
        isDel: null,
        reciveOrderDetailList: [],
        approvalUser: {userName: ""},
      };
      this.resetForm("form");
      this.resetForm("querySaleOrderForm");
      this.saleOrderDetailList = [];
      this.tempDetailList = [];
      this.deleteIds = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderDate = [];
      this.daterangeEffectDate = [];
      this.daterangeCreateDate = [];
      this.daterangeModifyDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增销售收款单按钮操作 */
    handleAddSale() {
      this.reset();
      this.form.orderCode = "CWSK" + Date.now();
      this.form.paymentType = 8;
      this.open = true;
      this.title = "添加销售收款单";
      this.formDisabled = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getReciveOrder(row);
      this.open = true;
      this.title = "修改财务收款单";
      this.formDisabled = false;
    },
    /** 显示详情按钮操作 */
    showReciveOrder(row) {
      this.reset();
      this.getReciveOrder(row);
      this.open = true;
      this.title = "查看财务收款单";
      this.formDisabled = true;
    },
    /** 查询财务收款单详情 */
    getReciveOrder(row) {
      const id = row.id || this.ids;
      getReciveOrder(id).then((response) => {
        this.form = response.data;
        if (this.form.approvalUser == null) {
          this.form.approvalUser = {userName: ""};
        }
        const codes = this.form.reciveOrderDetailList.map(
          (item) => item.srcOrderCode
        );
        return request({
          url: '/xiaoshou/saleBack/getSaleBackListByOrderCodes/' + codes,
          method: 'get'
        }).then((response) => {
          this.saleOrderDetailList = response.data;
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.reciveOrderDetailList.forEach((item) => {
        let isSubmit = this.validateMoney(item);
        if (!isSubmit) {
          return;
        }
      });
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateReciveOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("form", this.form)
            addReciveOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除财务收款单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delReciveOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "finance/reciveOrder/export",
        {
          ...this.queryParams,
        },
        `reciveOrder_${new Date().getTime()}.xlsx`
      );
    },
    // 子表单选择器
    handleDetailSelect(key, keyPath) {
      this.activeIndex = key;
    },

    //显示销售出库单
    showSaleOrder() {
      this.innerVisible = true;
      this.getSaleOrderList();
    },
    // 子表单新增
    handleDetailAdd() {
      //添加数据，并禁用表单
      this.tempDetailList.forEach((item) => {
        if (
          !this.saleOrderDetailList.some(
            (saleOrder) => saleOrder.id === item.id
          )
        ) {
          this.saleOrderDetailList.push({...item});
          this.form.reciveOrderDetailList.push({
            ...item,
            money: 0,
            remark: "",
            accountId: null,
            payTypeId: null,
            srcOrderCode: item.saleBackCode,
          });
        }
      });
      this.innerVisible = false;
      this.clearSelection("saleOrderTable");
    },
    // 子表单删除
    handleDetailDelete() {
      // 删除选择的行及入库单明细
      this.deleteIds.forEach((id) => {
        this.saleOrderDetailList = this.saleOrderDetailList.filter(
          (order) => order.id !== id
        );
        this.form.reciveOrderDetailList =
          this.form.reciveOrderDetailList.filter((order) => order.id !== id);
        this.tempDetailList = this.tempDetailList.filter(
          (order) => order.id !== id
        );
      });
      this.deleteIds = [];
    },
    // 采购退货明细多级选择器
    handleDetailSelectionChange(val) {
      this.detailsSelection = val;
      this.orderMultiple = !val.length;

      this.deleteIds = val.map((row) => row.id);
    },
    //重置销售出库单搜索条件
    resetSaleOrderForm() {
      this.querySaleOrderParams = {
        orderCode: null,
        orderDate: null,
      };
      this.resetForm("saleOrderForm");
    },
    /** 内部销售出库单搜索按钮操作 */
    handleSaleOrderQuery() {
      this.querySaleOrderParams.pageNum = 1;
      this.getSaleOrderList();
    },
    /** 重置按钮操作 */
    resetSaleOrderQuery() {
      this.daterangeSaleOrderCreateDate = [];
      this.resetForm("querySaleOrderForm");
      this.handleSaleOrderQuery();
    },
    // 内部销售出库单多选框选中数据
    handleSaleOrderSelectionChange(selection) {
      selection.forEach((item) => {
        if (
          !this.tempDetailList.some((saleOrder) => saleOrder.id === item.id)
        ) {
          this.tempDetailList.push({...item});
        }
      });
    },
    /** 查询内部销售出库单列表 */
    getSaleOrderList() {
      this.saleOrderLoading = true;
      this.querySaleOrderParams.params = {};
      this.customerList.forEach((item) => {
        if (item.id == this.form.customerId) {
          this.querySaleOrderParams.cusName = item.cusName;
        }
      });
      if (
        null != this.daterangeSaleOrderCreateDate &&
        "" != this.daterangeSaleOrderCreateDate
      ) {
        this.querySaleOrderParams.params["beginCreateDate"] =
          this.daterangeSaleOrderCreateDate[0];
        this.querySaleOrderParams.params["endCreateDate"] =
          this.daterangeSaleOrderCreateDate[1];
      }
      return request({
        url: '/xiaoshou/saleBack/listForReciveOrder',
        method: 'get',
        params: this.querySaleOrderParams
      }).then((response) => {
        this.saleOrderList = response.rows;
        this.saleOrderTotal = response.total;
        this.saleOrderLoading = false;
      });
    },
    // 清空选择状态
    clearSelection(ref) {
      this.$refs[ref].clearSelection();
    },
    // 关闭内部销售出库单对话框
    cancelInner() {
      this.innerVisible = false;
    },
    // 动态表单校验规则
    validateMoney(row) {
      if (isNaN(row.money) || row.money == null) {
        this.$message.error("请输入数字");
        row.money = null;
        return false;
      }
      let value = parseFloat(row.money);
      let max = row.total - row.payMoney;
      if (value <= 0) {
        this.$message.error("金额不能小于0");
        row.money = null;
        return false;
      } else if (value > max) {
        this.$message.error(`金额不能大于${max}`);
        row.money = max;
        return false;
      } else {
        // 四舍五入并保留两位小数
        value = value.toFixed(2);

        // 更新金额字段为格式化后的值
        row.money = value;
        return true;
      }
    },
    // 显示表单详细信息
    showFormDetail() {
      this.formDetailDisabled = !this.formDetailDisabled;
    },
  },
};
</script>
<style scoped>
/**  长度过长的文本省略号 */
.ellipsis-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100px;
}
</style>
